/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.base.service.warehouse;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.base.dao.warehouse.TwarehouseChargeItemsDao;
import com.jeesite.modules.base.dao.warehouse.TwarehouseDao;
import com.jeesite.modules.base.dao.warehouse.TwarehouseOtherChargeItemsDao;
import com.jeesite.modules.base.entity.warehouse.Twarehouse;
import com.jeesite.modules.base.entity.warehouse.TwarehouseChargeItems;
import com.jeesite.modules.base.entity.warehouse.TwarehouseOtherChargeItems;
import com.jeesite.modules.custom.service.CodeRuleService;
import com.jeesite.modules.file.utils.FileUploadUtils;
import com.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 仓库档案Service
 *
 * @author shengbin
 * @version 2021-03-02
 */
@Service
@Transactional(readOnly = true)
public class TwarehouseService extends CrudService < TwarehouseDao, Twarehouse > {

    @Autowired
    private TwarehouseOtherChargeItemsDao twarehouseOtherChargeItemsDao;
    @Autowired
    private CodeRuleService codeRuleService;
    @Autowired
    private TwarehouseChargeItemsDao twarehouseChargeItemsDao;

    /**
     * 获取单条数据
     *
     * @param twarehouse
     * @return
     */
    @Override
    public Twarehouse get(Twarehouse twarehouse) {
        Twarehouse entity = super.get(twarehouse);
        if (entity != null) {
            TwarehouseOtherChargeItems twarehouseOtherChargeItems = new TwarehouseOtherChargeItems(entity);
            twarehouseOtherChargeItems.setStatus(TwarehouseOtherChargeItems.STATUS_NORMAL);
            entity.setTwarehouseOtherChargeItemsList(twarehouseOtherChargeItemsDao.findList(twarehouseOtherChargeItems));
            TwarehouseChargeItems twarehouseChargeItems = new TwarehouseChargeItems(entity);
            twarehouseChargeItems.setStatus(TwarehouseChargeItems.STATUS_NORMAL);
            entity.setTwarehouseChargeItemsList(twarehouseChargeItemsDao.findList(twarehouseChargeItems));
        }
        return entity;
    }

    /**
     * 查询分页数据
     *
     * @param twarehouse      查询条件
     * @param twarehouse.page 分页对象
     * @return
     */
    @Override
    public Page < Twarehouse > findPage(Twarehouse twarehouse) {
        return super.findPage(twarehouse);
    }

    /**
     * 保存数据（插入或更新）
     *
     * @param twarehouse
     */
    @Override
    @Transactional(readOnly = false)
    public void save(Twarehouse twarehouse) {
        if (twarehouse.getIsNewRecord()) {
            twarehouse.setWarehouseId(codeRuleService.getCode("warehouse"));
            twarehouse.setCreateBy(UserUtils.getUser().getUserCode());
            twarehouse.setUpdateBy(UserUtils.getUser().getUserCode());
        }else {
            twarehouse.setUpdateBy(UserUtils.getUser().getUserCode());
        }
        super.save(twarehouse);
        if (twarehouse.getIsNewRecord()) {
            codeRuleService.updateCurrentNumber("warehouse");
        }
        // 保存上传附件
        FileUploadUtils.saveFileUpload(twarehouse.getId(), "twarehouse_file");
        // 保存 Twarehouse子表
        for (TwarehouseOtherChargeItems twarehouseOtherChargeItems : twarehouse.getTwarehouseOtherChargeItemsList()) {
            if (!TwarehouseOtherChargeItems.STATUS_DELETE.equals(twarehouseOtherChargeItems.getStatus())) {
                twarehouseOtherChargeItems.setWarehouseId(twarehouse);
                if (twarehouseOtherChargeItems.getIsNewRecord()) {
                    twarehouseOtherChargeItemsDao.insert(twarehouseOtherChargeItems);
                } else {
                    twarehouseOtherChargeItemsDao.update(twarehouseOtherChargeItems);
                }
            } else {
                twarehouseOtherChargeItemsDao.delete(twarehouseOtherChargeItems);
            }
        }
        // 保存 Twarehouse子表
        for (TwarehouseChargeItems twarehouseChargeItems : twarehouse.getTwarehouseChargeItemsList()) {
            if (!TwarehouseChargeItems.STATUS_DELETE.equals(twarehouseChargeItems.getStatus())) {
                twarehouseChargeItems.setWarehouseId(twarehouse);
                if (twarehouseChargeItems.getIsNewRecord()) {
                    twarehouseChargeItemsDao.insert(twarehouseChargeItems);
                } else {
                    twarehouseChargeItemsDao.update(twarehouseChargeItems);
                }
            } else {
                twarehouseChargeItemsDao.delete(twarehouseChargeItems);
            }
        }
    }

    /**
     * 更新状态
     *
     * @param twarehouse
     */
    @Override
    @Transactional(readOnly = false)
    public void updateStatus(Twarehouse twarehouse) {
        super.updateStatus(twarehouse);
    }

    /**
     * 删除数据
     *
     * @param twarehouse
     */
    @Override
    @Transactional(readOnly = false)
    public void delete(Twarehouse twarehouse) {
        super.delete(twarehouse);
        TwarehouseOtherChargeItems twarehouseOtherChargeItems = new TwarehouseOtherChargeItems();
        twarehouseOtherChargeItems.setWarehouseId(twarehouse);
        twarehouseOtherChargeItemsDao.deleteByEntity(twarehouseOtherChargeItems);
        TwarehouseChargeItems twarehouseChargeItems = new TwarehouseChargeItems();
        twarehouseChargeItems.setWarehouseId(twarehouse);
        twarehouseChargeItemsDao.deleteByEntity(twarehouseChargeItems);
    }

}